package com.nit.lab.controller.ship;


import com.alibaba.fastjson.JSONObject;
import com.nit.lab.dto.ship.ShipDTO;
import com.nit.lab.dto.ship.WorkDTO;
import com.nit.lab.dto.system.TokenDTO;
import com.nit.lab.dto.utils.OpResultDTO;
import com.nit.lab.dto.utils.TableReqDTO;
import com.nit.lab.dto.utils.TableRspDTO;
import com.nit.lab.service.ship.ShipService;
import com.nit.lab.service.ship.WorkService;
import com.nit.lab.utils.HttpRequestReader;
import com.nit.lab.utils.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/api/work")
public class WorkController {

    @Resource
    private WorkService workService;
    private final Logger logger = LoggerFactory.getLogger(WorkController.class);

    @RequestMapping(value = "query", method = RequestMethod.POST)
    public TableRspDTO query(@RequestHeader(name = "Access-Token") String token,@RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            tableRspDTO=workService.query(tableReqDTO);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableRspDTO;
    }


    @RequestMapping(value = "queryWorkType", method = RequestMethod.POST)
    public OpResultDTO query(@RequestHeader(name = "Access-Token") String token) {
        OpResultDTO opResultDTO = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            opResultDTO=workService.queryWorkType();
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return opResultDTO;
    }


    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public OpResultDTO add(@RequestHeader(name = "Access-token") String token,@RequestBody WorkDTO workDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            opResult.setMessage("success");
            opResult.setResult(workService.add(workDTO));
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public OpResultDTO update(@RequestHeader(name = "Access-token") String token,@RequestBody WorkDTO workDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            opResult.setMessage("success");
            Integer n = workService.update(workDTO);
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public OpResultDTO remove(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonObject = HttpRequestReader.getJsonObject(request);
            String workIds = jsonObject.getString("workIds");
            opResult.setMessage(workService.remove(workIds).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }
}
